/**
 * Project Name:mauto-web
 * File Name:PeopleController.java
 * Package Name:com.inftech.mauto.sys.web
 * Date:2016年11月9日下午12:25:36
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.sys.web.front;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.inftech.mauto.common.base.BaseController;
import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.constant.SessionConstEnum;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.common.utils.SendSmsUtil;
import com.inftech.mauto.core.entity.Car;
import com.mingsoft.people.dao.IPeopleDao;
import com.mingsoft.people.entity.PeopleEntity;
import com.mingsoft.people.entity.PeopleUserEntity;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

/**
 * ClassName:ApiPeopleController <br/>
 * Function: 用户相关控制层. <br/>
 * Date: 2016年11月9日 下午12:25:36 <br/>
 * 
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/api/people")
public class ApiPeopleController extends BaseController {

	/**
	 * 用户持久化层
	 */
	@Autowired
	private IPeopleDao peopleDao;

	/**
	 * 
	 * bind:绑定手机号. <br/>
	 *
	 * @author puyf
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/bind", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String bind(@RequestBody String requestStr, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			JSONObject jsonObject = (JSONObject) JSONObject.parse(requestStr);
			// 用户手机号码
			String peoplePhone = jsonObject.getString("peoplePhone");
			// 验证码
			String validateCode = jsonObject.getString("validateCode");
			if (!peoplePhone.equals(this.getSession(request, SessionConstEnum.VALIDATE_PHONE_SESSION))
					|| !validateCode.equals(this.getSession(request, SessionConstEnum.VALIDATE_CODE_SESSION))) {
				// 验证码不正确
				LOG.warn("绑定手机失败，验证码不正确");
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_931;
				outJson = getOutJson(statusCode, false, statusCode.toString(), null);
			} else {
				// 验证码正确
				// 获取用户对象
				WeixinPeopleEntity weixinPeople = (WeixinPeopleEntity) getSession(request,
						SessionConstEnum.PEOPLE_SESSION);
				weixinPeople.setPeoplePhone(peoplePhone);
				// 更新用户信息
				peopleDao.updateEntity(weixinPeople);
				LOG.info("绑定手机成功");
				// 返回结果
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
				outJson = getOutJson(statusCode, true, statusCode.toString(), null);
			}
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			LOG.error("绑定手机失败", e);
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;

	}

	/**
	 * 
	 * getCode:获取校验码. <br/>
	 *
	 * @author puyf
	 * @param peoplePhone
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/getCode/{peoplePhone}", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getCode(@PathVariable String peoplePhone, HttpServletRequest request) throws Exception {
		String outJson = "";
		int validateCode = CommonUtils.getRandomInt();
		String smsContent = "您好，您的验证码是" + validateCode + "，欢迎使用米拓车服";
		SendSmsUtil.batchSend(peoplePhone, smsContent);
		setSession(request, SessionConstEnum.VALIDATE_PHONE_SESSION, String.valueOf(peoplePhone));
		setSession(request, SessionConstEnum.VALIDATE_CODE_SESSION, String.valueOf(validateCode));
		HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
		outJson = getOutJson(statusCode, true, statusCode.toString(), null);
		LOG.info("获取验证码成功，验证码为" + validateCode);
		return outJson;
	}

	/**
	 * 
	 * contactDetail:(获取联系信息 通过peopleId). <br/>
	 *
	 * @author Administrator
	 * @param peoplePhone
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/contactDetail", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String contactDetail(@PathVariable String peoplePhone, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			WeixinPeopleEntity people = (WeixinPeopleEntity) getSession(request, SessionConstEnum.PEOPLE_SESSION);
			PeopleEntity peopleEntity = peopleDao.getContactDetailByPeopleId(people.getPeopleId());
			PeopleUserEntity peopleUserEntity = peopleEntity.getPeopleUser();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("contactName", peopleUserEntity.getPeopleUserRealName());
			if (peopleUserEntity.getPeopleUserSex() == 1) {
				resultMap.put("contactSex", "男");
			}
			if (peopleUserEntity.getPeopleUserSex() == 2) {
				resultMap.put("contactSex", "女");
			}
			if (peopleUserEntity.getPeopleUserSex() == 0) {
				resultMap.put("contactSex", "未知");
			}
			resultMap.put("contactPhone", peopleEntity.getPeoplePhone());
			resultMap.put("contactAdress", peopleUserEntity.getPeopleUserAddress());
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
			outJson = getOutJson(statusCode, true, statusCode.toString(), resultMap);
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

	/**
	 * 
	 * saveContact:(保存联系信息). <br/>
	 *
	 * @author Administrator
	 * @param peoplePhone
	 * @param request
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/saveContact", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveContact(@PathVariable String peoplePhone, HttpServletRequest request) throws Exception {
		String outJson = "";
		try {
			Enumeration<String> enumeration = request.getParameterNames();
			String name = "";
			String value = "";
			Map<String, Object> paramMap = new HashMap<String, Object>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				paramMap.put(name, value);
			}
			int row = peopleDao.updatePeople(paramMap);
			if (row == 1) {
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.STATUS_CODE_200;
				outJson = getOutJson(statusCode, true, statusCode.toString(), null);
			} else {
				HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
				outJson = getOutJson(statusCode, false, statusCode.toString(), null);
			}
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				throw e;
			}
			HttpStatusCodeEnum statusCode = HttpStatusCodeEnum.SCOPE_BASE;
			outJson = getOutJson(statusCode, false, statusCode.toString(), null);
		}
		return outJson;
	}

}
